import { getPermissionList } from '@/api/auth'
import { IMenuItem } from '@/types/menu'

export interface IAuthLoader {
  buttonList: string[]
  menuList: IMenuItem[]
  menuPathList: string[]
}

// 获取权限列表
export default async function AuthLoader() {
  const { data } = await getPermissionList()
  const menuPathList = getMenuPathList(data.menuList)
  return { buttonList: data.buttonList, menuList: data.menuList, menuPathList }
}

const getMenuPathList = (list: IMenuItem[]): string[] => {
  return list.reduce((result: string[], item) => {
    return result.concat(item.children?.length && !item.buttons ? getMenuPathList(item.children) : item.path + '')
  }, [])
}
